package com.ruoyi.common.enums.comlbs;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 车辆状态枚举类
 */
@NoArgsConstructor
@AllArgsConstructor
@Getter
public enum VehicleStatusEnum {

    DRIVING(0, "行驶"),
    STOP(1, "停留"),
    FLAME_OUT(2, "熄火"),
    POSITIONING_FAIL(3, "定位失败"),
    OFFLINE(4, "离线"),
    NOT_LOCATE(5, "未定位"),
    ONLINE(6, "在线"),
    ;

    public final static Integer offlineTime = 300000;

    private int code;
    private String text;

    // 创建code到枚举的映射表（静态初始化）
    private static final Map<Integer, VehicleStatusEnum> CODE_MAP = Arrays.stream(values())
            .collect(Collectors.toMap(VehicleStatusEnum::getCode, e -> e));

    /**
     * 根据状态码获取对应的中文描述
     * @param code 状态码
     * @return 对应的中文描述，如果找不到返回"未知状态"
     */
    public static String getTextByCode(int code) {
        VehicleStatusEnum status = CODE_MAP.get(code);
        return status != null ? status.getText() : "未知状态";
    }

    /**
     * 根据状态码获取枚举实例
     * @param code 状态码
     * @return 对应的枚举实例，如果找不到返回null
     */
    public static VehicleStatusEnum getByCode(int code) {
        return CODE_MAP.get(code);
    }

}
